package com.zhentao.es.service.impl;

import com.zhentao.es.pojo.ApUserSearch;
import com.zhentao.es.pojo.dto.HistorySearchDto;
import com.zhentao.es.service.ApUserSearchService;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApUserSearchServiceImpl implements ApUserSearchService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Async
    @Override
    public void insert(String keyword, Integer userId) {
        //1.查看当前用户搜索的关键词是否存在
        Query query=new Query(Criteria.where("userId").is(userId).and("keyword").is(keyword));
        ApUserSearch apUserSearch = mongoTemplate.findOne(query, ApUserSearch.class, "ap_user_search");

        //2.如果存在，更新一下创建时间
        if(apUserSearch !=null){
            Update update=new Update().set("createTime", new Date());
            mongoTemplate.updateFirst(query,update,ApUserSearch.class);
            return;
        }


        //3.如果不存在，判断当前历史记录的总条数是否大于10
        ApUserSearch userSearch=new ApUserSearch();
        userSearch.setUserId(userId);
        userSearch.setKeyword(keyword);
        userSearch.setCreatedTime(new Date());

        Query query1=new Query(Criteria.where("userId").is(userId));
        //根据时间降序
        query1.with(Sort.by(Sort.Direction.DESC,"createTime"));
        List<ApUserSearch> apUserSearches = mongoTemplate.find(query1, ApUserSearch.class);

        System.out.println(apUserSearches.size()+"size");
        if(apUserSearches == null || apUserSearches.size() < 10){
            //如果没有或者小于10条，新增
            mongoTemplate.save(userSearch);
        } else {
            //已经有10条了，把最后一条替换掉
            ApUserSearch laSearch = apUserSearches.get(apUserSearches.size() - 1);
            mongoTemplate.findAndReplace(Query.query(Criteria.where("id").is(laSearch.getId())),userSearch);
        }
    }

    @Override
    public ResponseResult findUserSearch() {
        //id也先给固定住
        Query query=new Query(Criteria.where("userId").is(4));
        //按照时间倒叙
        query.with(Sort.by(Sort.Direction.DESC,"createTime"));
        List<ApUserSearch> apUserSearchList = mongoTemplate.find(query, ApUserSearch.class);
        return ResponseResult.okResult(apUserSearchList);
    }

    @Override
    public ResponseResult delUserSearch(HistorySearchDto dto) {
        if(dto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断是否登录

        return null;
    }
}
